const { think } = require('thinkjs');
const Base = require('./base.js');
const { default: BigNumber } = require('bignumber.js');
const moment = require('moment');

module.exports = class extends Base {
  async listInAction() {
    const page = this.get('page') || 1;
    const size = this.get('size') || 20;

    const query = this.get();

    if (query.company) {
      query.company = ['like', `%${query.company}%`];
    }

    delete query.page;
    delete query.size;

    const data = await this.model('bill_list')
      .where(Object.assign(query, { inout: 1 }))
      .order(['id DESC'])
      .page(page, size)
      .countSelect();

    return this.success(data);
  }

  async listOutAction() {
    const page = this.get('page') || 1;
    const size = this.get('size') || 20;

    const query = this.get();

    if (query.company) {
      query.company = ['like', `%${query.company}%`];
    }

    delete query.page;
    delete query.size;

    const data = await this.model('bill_list')
      .where(Object.assign(query, { inout: 2 }))
      .order(['id DESC'])
      .page(page, size)
      .countSelect();

    return this.success(data);
  }

  async putAction() {
    const values = this.post();
    const id = this.post('id');
    const model = this.model('bill_list');

    if (id > 0) {
      await model.where({ id: id }).update(values);
      this.model('addLog').add({
        user_id: this.getLoginUserId(),
        title: '编辑发票',
        content: id,
      });
    } else {
      delete values.id;

      values.create_time = ['exp', 'CURRENT_TIMESTAMP()'];

      const addId = await model.add(values);

      this.model('addLog').add({
        user_id: this.getLoginUserId(),
        title: '新建发票',
        content: addId,
      });
    }

    return this.success(values);
  }

  async delAction() {
    const id = this.get('id');
    const d = await this.model('bill_list')
      .where({ id: id })
      .find();
    await this.model('bill_list')
      .where({ id: id })
      .limit(1)
      .delete();

    this.model('addLog').add({
      user_id: this.getLoginUserId(),
      title: '删除发票',
      content: JSON.stringify(d),
    });

    return this.success();
  }

  async taxAction() {
    const page = this.get('page') || 1;
    const size = this.get('size') || 20;

    const query = this.get();

    delete query.page;
    delete query.size;

    const data = await this.model('tax_list')
      .where(query)
      .order(['id DESC'])
      .page(page, size)
      .countSelect();

    return this.success(data);
  }

  async putTaxAction() {
    const values = this.post();
    const id = this.post('id');
    const model = this.model('tax_list');

    if (id > 0) {
      await model.where({ id: id }).update({
        prev: values.prev,
      });
      this.model('addLog').add({
        user_id: this.getLoginUserId(),
        title: '编辑shui',
        content: id,
      });
    } else {
      // delete values.id;
      // values.create_time = ['exp', 'CURRENT_TIMESTAMP()'];
      // const addId = await model.add(values);
      // this.model('addLog').add({
      //   user_id: this.getLoginUserId(),
      //   title: '新建发票',
      //   content: addId,
      // });
    }

    return this.success(values);
  }

  async taxFreshAction() {
    const month = moment().format('YYYY-MM');

    const query = {
      update_time: [
        'between',
        [
          moment()
            .startOf('month')
            .format('YYYY-MM-DD'),
          moment()
            .endOf('month')
            .format('YYYY-MM-DD'),
        ],
      ],
      account_id: this.get('account_id'),
    };
    const sumIn =
      (await this.model('bill_list')
        .where(Object.assign({ inout: 1 }, query))
        .sum('tax')) || 0;

    const sumOut =
      (await this.model('bill_list')
        .where(Object.assign({ inout: 2 }, query))
        .sum('tax')) || 0;
    const currentData = await this.model('tax_list')
      .where({
        status: 0,
        account_id: this.get('account_id'),
        month: month,
      })
      .find();

    const rest = BigNumber(currentData.prev)
      .plus(BigNumber(sumOut))
      .minus(BigNumber(sumIn))
      .toFixed();

    await this.model('tax_list')
      .where({
        account_id: this.get('account_id'),
        month: month,
      })
      .update({
        current_in: sumIn,
        current_out: sumOut,
        rest: rest,
      });
    return this.success();
  }
};
